package cn.tedu.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;


import java.io.IOException;

/**
 * @author Amos
 * @date 2022/5/10
 */

public class HdfsDemo {
    public static void main(String[] args) throws IOException {

        // 创建Configuration配置对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop01:8020");

        // 创建文件系统
        FileSystem fs = FileSystem.get(conf);
        System.out.println(fs);
    }

    /**
     * 创建一个路径 /test/input
     * @throws IOException
     */
    @Test
    public void mkdirPath() throws IOException {
        // 获取文件系统对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);
        fs.mkdirs(new Path("/test/input"));
        fs.close();
    }

    @Test
    public void putFiles() throws IOException {
        // 获取文件系统对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);

        // 上传文件
        fs.copyFromLocalFile(
                new Path("file:///C:/TCGBVI/tools/1.txt"),
                new Path("/test/input"));
        fs.close();
    }

    @Test
    public void getFiles() throws IOException {
        // 获取文件系统对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);

        fs.copyToLocalFile(new Path("/test/input/1.txt"),
                new Path("file:///C:/TCGBVI/tools/2.txt"));
        fs.close();
    }
}
